<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<pre>
    判断一个算术表达式的括号是否匹配
</pre>
<script>
    function Stack(){
        this.data=[];
        this.top=0;
    }
    Stack.prototype={
        constructor:Stack,
        push: function (elm) {
            this.data[this.top++]=elm;
        },
        pop: function () {
            return this.data[--this.top];
        },
        peek: function () {
            return this.data[this.top-1];
        },
        size: function () {
            return this.top;
        },
        clear: function () {
            delete this.data;
            this.data=[];
            this.top=0;
        }
    }

    function match(exp){
        var left=new Stack(),right=new Stack(),r='',lSize,rSize,max;
        for(var i=0;i<exp.length;i++){
            if('('==exp[i]){
                left.push('(');
            }else if(')'==exp[i]){
                right.push(')');
            }
        }
        console.log(left.data,right.data);
        lSize=left.size();
        rSize=right.size();
        if(lSize==rSize)return false;//括号未缺失
        max=Math.max(lSize,rSize);
        for(var i=0;i<max;i++){
            console.log(left.data[i],right.data[i]);
            if(
                    (!left.data[i])
                    &&
                    right.data[i]
            ){
                r+='第'+i+'个左括号缺失';
            }

            if(
                    left.data[i]
                    &&
                    (!right.data[i])
            ){
                r+='第'+i+'个右括号缺失';
            }
        }
        return r;
    }

    console.log(match('10*(1+2)/(3+10-4*(5+6)'));

</script>
</body>
</html>